 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-12-22  <br>
 */
package com.ce.pms.custservice.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import org.hibernate.SQLQuery;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.ce.pms.DBUnitTool;
import com.ce.pms.base.dao.IPmsBaseHibernateDao;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.custservice.service.IDFormManageService;
import com.ce.pms.custservice.vo.AbstractServiceItemVO;
import com.ce.pms.custservice.vo.DispatchFormSearchVO;
import com.ce.pms.custservice.vo.DispatchFormVO;
import com.ce.pms.custservice.vo.FeedbackRecordVO;
import com.ce.pms.custservice.vo.LaborItemVO;
import com.ce.pms.custservice.vo.ServItemSearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 	名称: com.ce.pms.system.custservice.service.DFormManageServiceT <br>
 *
 *
 * @author  付湘辉  <br>
 * Created on： 2008-12-22 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class DFormManageServiceImplT extends DBUnitTool {

	private static Long complateDForm=1l;//完工状态的派工单
	private static Long creator=1l;//创建人ID
	private static Long deptId=1l;//部门ID
	private static Long handlingDForm=8l;//处理中状态的派工单
	private static Long invaEventId=10l;//无效的事件
	private static Long newDForm=11l;//待处理状态的派工单

	/**
	 * 用工服务项目ID，已存在的
	 */
	private static Long serItmeId = 1l;

	/**
	 * 用工服务项目ID，不存在的
	 */
	private static Long serItmeIdnotExits = 1000000l;
	/**
	 * 有效的事件
	 */
	private static Long vaEventId=1l;

	private IDFormManageService dispatchFormManageService;
	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testAddDispatchForm() {
		try {
			initDFormManageService();
			//正常情况测试
			Long csEventID = vaEventId;//已存在的事件ID并且是有效的，已提交的事件
			DispatchFormVO dispatchFormVo = new DispatchFormVO();
			DispatchFormVO resDispatchFormVo = null;
			dispatchFormVo.setCreator(creator);
			dispatchFormVo.setContact("业主A");
			dispatchFormVo.setDeptId(deptId);
			dispatchFormVo.setDeptName("系统管理部");
			dispatchFormVo.setDispatchMan("superuser");
			dispatchFormVo.setEmergencyDegreeId(1l);
			dispatchFormVo.setHouseId(3l);
			dispatchFormVo.setHouseNumber("1-1A");
			dispatchFormVo.setServiceContent("服务内容");
			resDispatchFormVo = dispatchFormManageService.addDispatchForm(csEventID, dispatchFormVo);
			Assert.assertNotNull(resDispatchFormVo);
			Assert.assertNotNull(resDispatchFormVo.getKeyword());
			Assert.assertFalse(BlankUtil.isBlank(resDispatchFormVo.getDispatchNo()));

			Long keyword = resDispatchFormVo.getKeyword();
			Long taskId = resDispatchFormVo.getTaskId();

			//数据清理
			IPmsBaseHibernateDao pmsBaseHibernateDao =  (IPmsBaseHibernateDao)applicationContext.getBean("pmsBaseHibernateDao");
			String sql = "DELETE FROM CE_DISPATCH_FORM WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			// 删除新增派工单记录
			pmsBaseHibernateDao.executeSQL(sql, new Object[]{keyword});
			sql = "DELETE FROM CE_DISPATCH_EVENT where CE_DISPATCH_EVENT.DISPATCH_EVENT_ID=? ";
			// 删除新增任务记录
			pmsBaseHibernateDao.executeSQL(sql, new Object[]{taskId});
			pmsBaseHibernateDao.flush();
			sql = "SELECT * FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			SQLQuery sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, keyword);
			Assert.assertNull(sqlQuery.uniqueResult());

			// 事件不是有效的
			csEventID = invaEventId;
			dispatchFormVo = new DispatchFormVO();
			dispatchFormVo.setCreator(creator);
			dispatchFormVo.setContact("qrdfsdf");
			dispatchFormVo.setDeptId(deptId);
			dispatchFormVo.setDeptName("系统管理部");
			dispatchFormVo.setDispatchMan("superuser");
			dispatchFormVo.setEmergencyDegreeId(1l);
			dispatchFormVo.setHouseId(14l);
			dispatchFormVo.setHouseNumber("1-1B");
			dispatchFormVo.setServiceContent("服务内容");
			resDispatchFormVo = null;
			try {
				resDispatchFormVo = dispatchFormManageService.addDispatchForm(csEventID, dispatchFormVo);
				Assert.fail("Because the event is not valid, the creation of dispatchForm were not successful.");
			} catch (BusinessException be) {
				this.dealBusinessException(be, "custservice:custservice_cannot_adddform_exception");
			}

			//	事件是有效的,服务内容字段没有传入值
			csEventID = vaEventId;
			dispatchFormVo = new DispatchFormVO();
			dispatchFormVo.setCreator(creator);
			dispatchFormVo.setContact("业主A");
			dispatchFormVo.setDeptId(deptId);
			dispatchFormVo.setDeptName("系统管理部");
			dispatchFormVo.setDispatchMan("superuser");
			dispatchFormVo.setEmergencyDegreeId(1l);
			dispatchFormVo.setHouseId(3l);
			dispatchFormVo.setHouseNumber("1-1A");
//			dispatchFormVo.setServiceContent("服务内容");
			resDispatchFormVo = null;
			try {
				resDispatchFormVo = dispatchFormManageService.addDispatchForm(csEventID, dispatchFormVo);
				Assert.fail("Required items to be imported. ");
			} catch (BusinessException be) {
				this.dealBusinessException(be, "common:mustFill_null_exception");
			}
		} catch (Exception e) {
			this.dealException(e);
		}
	}
	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Test
	public void testAddFeedback() {
		try {
			initDFormManageService();
			//＝＝＝＝＝正常业务测试，反馈登记没有完工，没有填写开工时间
			Long dispatchFormID = handlingDForm;//处理中状态的派工单
			IPmsBaseHibernateDao pmsBaseHibernateDao =  (IPmsBaseHibernateDao)applicationContext.getBean("pmsBaseHibernateDao");

			FeedbackRecordVO feedbackVo = new FeedbackRecordVO();
			FeedbackRecordVO resFeedbackVo=null;
			feedbackVo.setCreator(creator);
			feedbackVo.setDispatchFormId(dispatchFormID);
			feedbackVo.setHasComplete("0");//未完工
			feedbackVo.setContent("一次反馈。");
//			执行
			resFeedbackVo=dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			Assert.assertNotNull(resFeedbackVo);
//			System.out.println("resFeedbackVo-keyword:"+resFeedbackVo.getKeyword());
			String sql = "SELECT LAST_INSERT_ID()";
			SQLQuery sqlQuery=pmsBaseHibernateDao.createSQLQuery(sql);
			Object temp = sqlQuery.uniqueResult();
			Long lastId = temp==null?null:((BigInteger)temp).longValue();
			Assert.assertNotNull(lastId);
			Long feedbackId = lastId;

			//清理数据
			sql = "DELETE FROM CE_FEED_BACK_RECORD WHERE FEED_BACK_RECORD_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, feedbackId);
			sqlQuery.executeUpdate();

			//＝＝＝＝＝反馈登记完工，没有填写开工时间
			sql = "SELECT CDF.STATUZ,CDF.DISPATCH_TIME,CDF.START_TIME,CDF.END_TIME FROM CE_DISPATCH_FORM CDF WHERE CDF.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dispatchFormID);
			Object[] objs = (Object[])sqlQuery.uniqueResult();

			String statuz = objs[0].toString();
			Date dispatchTime = objs[1]==null?null:(Date) objs[1];//派单时间
			Date startTime = objs[2]==null?null:(Date) objs[2];//开工时间
			Date endTime = objs[3]==null?null:(Date) objs[3];//完工时间
			Assert.assertTrue("002".equals(statuz));

			feedbackVo = new FeedbackRecordVO();
			resFeedbackVo=null;
			feedbackVo.setCreator(creator);
			feedbackVo.setDispatchFormId(dispatchFormID);
			feedbackVo.setHasComplete("1");//置为完工
			feedbackVo.setContent("一次反馈。");
			feedbackVo.setEndTime(DateUtil.addDate(startTime==null?dispatchFormManageService.getDate():startTime,5) );//开工时间后5天或当前时间后5天

			//执行
			FeedbackRecordVO resFeedbackRecordVO =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			Assert.assertNotNull(resFeedbackRecordVO);
			sql = "SELECT LAST_INSERT_ID()";
			sqlQuery=pmsBaseHibernateDao.createSQLQuery(sql);
			Object temp1 = sqlQuery.uniqueResult();
			Long lastId1 = temp1==null?null:((BigInteger)temp1).longValue();
			Assert.assertNotNull(lastId1);
			sql = "SELECT CE_DISPATCH_FORM.STATUZ FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dispatchFormID);
			Object obj = sqlQuery.uniqueResult();
			Assert.assertTrue("003".equals(obj.toString()));
			Assert.assertFalse(statuz.equals(obj.toString()));

			//清理数据
			sql = "DELETE FROM CE_FEED_BACK_RECORD WHERE FEED_BACK_RECORD_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, lastId1);
			sqlQuery.executeUpdate();
			//还原派工单数据
			sql = "UPDATE CE_DISPATCH_FORM CDF SET CDF.STATUZ='002',CDF.DISPATCH_TIME=?,CDF.START_TIME=?,CDF.END_TIME=? WHERE CDF.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setDate(0, dispatchTime);
			sqlQuery.setDate(1, startTime);
			sqlQuery.setDate(2, endTime);
			sqlQuery.setLong(3, dispatchFormID);
			sqlQuery.executeUpdate();

			//＝＝＝＝＝反馈登记完工，填有开工时间
			sql = "SELECT CDF.STATUZ,CDF.DISPATCH_TIME,CDF.START_TIME,CDF.END_TIME FROM CE_DISPATCH_FORM CDF WHERE CDF.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dispatchFormID);
			Object[] objs1 = (Object[])sqlQuery.uniqueResult();
			Assert.assertTrue("002".equals(objs1[0].toString()));
			Date dispatchTime1 = objs1[1]==null?null:(Date) objs1[1];//派单时间
			Date startTime1 = objs1[2]==null?null:(Date) objs1[2];//开工时间
			Date endTime1 = objs1[3]==null?null:(Date) objs1[3];//完工时间

			feedbackVo = new FeedbackRecordVO();
			resFeedbackVo=null;
			feedbackVo.setCreator(creator);
			feedbackVo.setDispatchFormId(dispatchFormID);
			feedbackVo.setHasComplete("1");//置为完工
			feedbackVo.setContent("一次反馈。");
//			如果已有开工时间，则使用，否则按这样的规则：派单时间后1天或当前时间
			feedbackVo.setStartTime(startTime1==null?dispatchTime1==null?dispatchFormManageService.getDate():DateUtil.addDate(dispatchTime1, 1):startTime1);
			feedbackVo.setEndTime(DateUtil.addDate(feedbackVo.getStartTime(),5) );//开工时间后5天
			//执行
			FeedbackRecordVO resFeedbackRecordVO1 =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			Assert.assertNotNull(resFeedbackRecordVO1);
			sql = "SELECT LAST_INSERT_ID()";
			sqlQuery=pmsBaseHibernateDao.createSQLQuery(sql);
			Object temp2 = sqlQuery.uniqueResult();
			Long lastId2 = temp2==null?null:((BigInteger)temp2).longValue();
			Assert.assertNotNull(lastId2);

			sql = "SELECT CE_DISPATCH_FORM.STATUZ FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dispatchFormID);
			String statuz4 = (String)sqlQuery.uniqueResult();

			Assert.assertTrue("003".equals(statuz4));
			Assert.assertFalse(objs1[0].equals(statuz4));

			//清理数据
			sql = "DELETE FROM CE_FEED_BACK_RECORD WHERE FEED_BACK_RECORD_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0,lastId2);
			sqlQuery.executeUpdate();
			//还原派工单数据
			sql = "UPDATE CE_DISPATCH_FORM CDF SET CDF.STATUZ='002',CDF.DISPATCH_TIME=?,CDF.START_TIME=?,CDF.END_TIME=? WHERE CDF.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setDate(0, dispatchTime1);
			sqlQuery.setDate(1, startTime1);
			sqlQuery.setDate(2, endTime1);
			sqlQuery.setLong(3, dispatchFormID);
			sqlQuery.executeUpdate();

			//=======================传入不合法参数
			try {
				//=======未完工，但反馈内容为空字符串
				feedbackVo = new FeedbackRecordVO();
				feedbackVo.setCreator(creator);
				feedbackVo.setDispatchFormId(dispatchFormID);
				feedbackVo.setHasComplete("0");//置为未完工
				feedbackVo.setContent("");
				// 执行
				FeedbackRecordVO resFeedbackRecordVO2 =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "common:mustFill_null_exception");
			}

			try {
				//=======未完工，但开工时间早于派单时间，前置条件：指定的派工单派单时间不为空
				sql = "SELECT CDF.STATUZ,CDF.DISPATCH_TIME,CDF.START_TIME,CDF.END_TIME FROM CE_DISPATCH_FORM CDF WHERE CDF.DISPATCH_FORM_ID=? ";
				sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
				sqlQuery.setLong(0, dispatchFormID);
				Object[] objs2 = (Object[])sqlQuery.uniqueResult();

				String statuz2 = objs[0].toString();
				Date dispatchTime2 = objs2[1]==null?null:(Date) objs2[1];//派单时间
				Date startTime2 = objs2[2]==null?null:(Date) objs2[2];//开工时间
				Date endTime2 = objs2[3]==null?null:(Date) objs2[3];//完工时间
				Assert.assertNotNull(dispatchTime2);

				feedbackVo = new FeedbackRecordVO();
				feedbackVo.setCreator(creator);
				feedbackVo.setDispatchFormId(dispatchFormID);
				feedbackVo.setHasComplete("0");//置为未完工
				feedbackVo.setContent("一次反馈。");
				feedbackVo.setStartTime(DateUtil.addDate(dispatchTime2, -1));
				// 执行
				FeedbackRecordVO resFeedbackRecordVO2 =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "custservice:custservice_starttiem_before_dispatchtime_exception");
			}

			try {
				//=======登记完工，但未填写完工时间
				sql = "SELECT CDF.STATUZ,CDF.DISPATCH_TIME,CDF.START_TIME,CDF.END_TIME FROM CE_DISPATCH_FORM CDF WHERE CDF.DISPATCH_FORM_ID=? ";
				sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
				sqlQuery.setLong(0, dispatchFormID);
				Object[] objs2 = (Object[])sqlQuery.uniqueResult();

				String statuz2 = objs[0].toString();
				Date dispatchTime2 = objs2[1]==null?null:(Date) objs2[1];//派单时间
				Date startTime2 = objs2[2]==null?null:(Date) objs2[2];//开工时间
				Date endTime2 = objs2[3]==null?null:(Date) objs2[3];//完工时间
				Assert.assertNotNull(dispatchTime2);

				feedbackVo = new FeedbackRecordVO();
				feedbackVo.setCreator(creator);
				feedbackVo.setDispatchFormId(dispatchFormID);
				feedbackVo.setHasComplete("1");//置为完工
				feedbackVo.setContent("一次反馈。");
				feedbackVo.setStartTime(DateUtil.addDate(dispatchTime2, 1));//派单时间后1天
				// 执行
				FeedbackRecordVO resFeedbackRecordVO3 =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "custservice:custservice_not_endtime_exception");
			}

			try {
				//=======登记完工，但完工时间早于开工时间
				sql = "SELECT CDF.STATUZ,CDF.DISPATCH_TIME,CDF.START_TIME,CDF.END_TIME FROM CE_DISPATCH_FORM CDF WHERE CDF.DISPATCH_FORM_ID=? ";
				sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
				sqlQuery.setLong(0, dispatchFormID);
				Object[] objs2 = (Object[])sqlQuery.uniqueResult();

				String statuz2 = objs[0].toString();
				Date dispatchTime2 = objs2[1]==null?null:(Date) objs2[1];//派单时间
				Date startTime2 = objs2[2]==null?null:(Date) objs2[2];//开工时间
				Date endTime2 = objs2[3]==null?null:(Date) objs2[3];//完工时间
				Assert.assertNotNull(dispatchTime2);

				feedbackVo = new FeedbackRecordVO();
				feedbackVo.setCreator(creator);
				feedbackVo.setDispatchFormId(dispatchFormID);
				feedbackVo.setHasComplete("1");//置为完工
				feedbackVo.setContent("一次反馈。");
				feedbackVo.setStartTime(DateUtil.addDate(dispatchTime2, 1));//派单时间后1天
				feedbackVo.setEndTime(DateUtil.addDate(feedbackVo.getStartTime(), -1));//完工时间早于开工时间
				// 执行
				FeedbackRecordVO resFeedbackRecordVO3 =dispatchFormManageService.addFeedback(dispatchFormID, feedbackVo);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "custservice:custservice_starttiem_after_endtime_exception");
			}
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetAbstractServiceItemById() {
		try {
			initDFormManageService();
			//查询已存在的项目
			AbstractServiceItemVO item = dispatchFormManageService.getAbstractServiceItemById(serItmeId);
			Assert.assertNotNull(item);

			// 查询不存在的项目
			AbstractServiceItemVO item2 = dispatchFormManageService.getAbstractServiceItemById(serItmeIdnotExits);
			Assert.assertNull(item2);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetDFormByID() {
		try {
			initDFormManageService();

			//正常业务
			DispatchFormVO dformVO = dispatchFormManageService.getDFormByID(complateDForm);
			Assert.assertNotNull(dformVO);
			//传入不合法参数
			//不存的
			dformVO = dispatchFormManageService.getDFormByID(0l);
			Assert.assertNull(dformVO);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetFeedbackRecordById() {
		try {
			initDFormManageService();
			FeedbackRecordVO feedback=dispatchFormManageService.getFeedbackRecordById(1l);
			Assert.assertNotNull(feedback);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-29  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testGetServItemTotalFee() {
		try {
			initDFormManageService();
			//正常查询
			BigDecimal totalFee = dispatchFormManageService.getServItemTotalFee(1l);
			Assert.assertNotNull(totalFee);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-22  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryDFromByPage() {
		try {
			initDFormManageService();
			DispatchFormSearchVO searchVO = new DispatchFormSearchVO();
			searchVO.setCustEventId(vaEventId);
			searchVO.setPageNo(1);
			searchVO.setPageSize(15);
			// 根据事件查
			Page page = dispatchFormManageService.queryDFromByPage(searchVO);
			Assert.assertNotNull(page);
			searchVO.setCustEventId(null);
			// 根据部门查
			searchVO.setDeptId(deptId);
			page = dispatchFormManageService.queryDFromByPage(searchVO);
			Assert.assertNotNull(page);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryServItemByPage() {
		try {
			initDFormManageService();
			ServItemSearchVO searchVO = new ServItemSearchVO();
			searchVO.setPageNo(1);
			searchVO.setPageSize(15);
			searchVO.setDispatchFormId(1l);
			Page page = dispatchFormManageService.queryServItemByPage(searchVO);
			Assert.assertNotNull(page);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-12-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testQueryServItemListByDFormId() {
		try {
			initDFormManageService();
			List<AbstractServiceItemVO>  list = dispatchFormManageService.queryServItemListByDFormId(1l);
			Assert.assertNotNull(list);
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	/**
	 * 功能描述:测试作废派工单  <br>
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@Ignore
	public void testSetAsideDForm() {
		try {
			initDFormManageService();
			//正常业务
			Long dFormID = newDForm;//新建状态的派工单ID
			IPmsBaseHibernateDao pmsBaseHibernateDao =  (IPmsBaseHibernateDao)applicationContext.getBean("pmsBaseHibernateDao");
			String sql = "SELECT STATUZ,REMARK FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			SQLQuery sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dFormID);
			Object[] objs = (Object[])sqlQuery.uniqueResult();
			Assert.assertTrue("001".equals(objs[0].toString()));
			String remark = objs[1]==null?null:objs[1].toString();
			String info = "作废 派工单"+ dFormID;

			Boolean res = dispatchFormManageService.setAsideDForm(dFormID, info);
			Assert.assertTrue(res);

			sql = "SELECT CE_DISPATCH_FORM.STATUZ FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dFormID);
			Object obj = sqlQuery.uniqueResult();
			Assert.assertTrue("000".equals(obj.toString()));

			// 数据清理
			sql = "UPDATE CE_DISPATCH_FORM SET STATUZ='001',REMARK=?  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setString(0, remark);
			sqlQuery.setLong(1, dFormID);
			sqlQuery.executeUpdate();
			pmsBaseHibernateDao.flush();
			//传入不合法参数
			info="";//备注信息为空字符串
			try {
				res=null;
				res = dispatchFormManageService.setAsideDForm(dFormID, info);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "custservice:custservice_dform_setaside_remarkisrequired_exception");
				sql = "SELECT CE_DISPATCH_FORM.STATUZ FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
				sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
				sqlQuery.setLong(0, dFormID);
				Object obj2 = sqlQuery.uniqueResult();
				//数据无变化
				Assert.assertTrue("001".equals(obj2.toString()));
			}

			dFormID=complateDForm;//派工单状态不是新建的
			sql = "SELECT CE_DISPATCH_FORM.STATUZ FROM CE_DISPATCH_FORM  WHERE CE_DISPATCH_FORM.DISPATCH_FORM_ID=? ";
			sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dFormID);
			Object obj2 = sqlQuery.uniqueResult();
			String statuz = obj2.toString();

			info="作废 "+dFormID;
			try {
				res=null;
				res = dispatchFormManageService.setAsideDForm(dFormID, info);
			} catch (BusinessException be) {
				super.dealBusinessException(be, "custservice:custservice_can_not_edit_dispatch_status");
				obj2 = sqlQuery.uniqueResult();
				//数据无变化
				Assert.assertTrue(statuz.equals(obj2.toString()));
			}
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	@Test
	public void testAddServItem() {
		try {
			initDFormManageService();
			IPmsBaseHibernateDao pmsBaseHibernateDao =  (IPmsBaseHibernateDao)applicationContext.getBean("pmsBaseHibernateDao");
			//正常新增测试
			Long dFormID = newDForm;//待处理状态的派工单
			//查询指定派工单的账单
			String sql = "SELECT CCSB.CUST_SERVICE_BILL_ID,CCSB.DEBIT FROM CE_CUST_SERVICE_BILL CCSB WHERE CCSB.DISPATCH_FORM_ID=? ";
			SQLQuery sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			sqlQuery.setLong(0, dFormID);
			Object[] objs = sqlQuery.uniqueResult()==null?null:(Object[])sqlQuery.uniqueResult();
			Long billId = objs==null?null:((BigInteger)objs[0]).longValue();
			BigDecimal billDebit = objs==null?null:(BigDecimal)objs[0];
			
			AbstractServiceItemVO servItemVo = new LaborItemVO();
			servItemVo.setDispatchFormId(dFormID);
			servItemVo.setName("修理门锁");
			servItemVo.setCode("0102");
			servItemVo.setRemark(servItemVo.getName());
			servItemVo.setCreator(creator);
			servItemVo.setCustomerUsage((short)10);
			servItemVo.setPrice(BigDecimal.valueOf(1000l, SysrefcodeDO.DATA_PRICE_SCALE));
			//执行业务方法
			dispatchFormManageService.addServItem(dFormID, servItemVo);
//			System.out.println("servItem-keyword:"+servItemVo.getKeyword());
			//得到ID
			// sql = "SELECT LAST_INSERT_ID()";
			// sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
			// Object temp1 = sqlQuery.uniqueResult();
			// Long servItemId =
			// temp1==null?null:((BigInteger)temp1).longValue();
			Long servItemId = servItemVo.getKeyword();
			Assert.assertNotNull(servItemId);
			//数据清理
			// 删除新增服务费用项目记录
			sql = "DELETE FROM CE_CUST_SERV_ITEM WHERE CUST_SERV_ITEM_ID=? ";
			pmsBaseHibernateDao.executeSQL(sql, new Object[]{servItemId});
			pmsBaseHibernateDao.flush();
			//恢复被更新应收金额的账单
			if (!BlankUtil.isBlank(billId)) {
				sql = "UPDATE CE_CUST_SERVICE_BILL SET DEBIT=? WHERE CUST_SERVICE_BILL_ID=? ";
				sqlQuery = pmsBaseHibernateDao.createSQLQuery(sql);
				sqlQuery.setBigDecimal(0, billDebit);
				sqlQuery.setLong(1, billId);
				sqlQuery.executeUpdate();
			}
			
		} catch (Exception e) {
			super.dealException(e);
		}
	}

	private void initDFormManageService(){
		if (dispatchFormManageService == null) {
			dispatchFormManageService = (IDFormManageService)applicationContext.getBean("dispatchFormManageService");
		}
	}
}
